import 'package:equatable/equatable.dart';
import 'package:floor/floor.dart';
import 'package:share_fit/app/helper/ext/tr_ext.dart';
import 'package:share_fit/app/models/base_model.dart';
import 'package:share_fit/generated/locales.g.dart';

@Entity(tableName: 'stress')
class StressModel extends BaseModel {
  /// 日期时间，格式 ：yyyy-MM-dd HH:mm
  @ColumnInfo(name: 'calendar_time')
  late String? calendarTime;

  /// 日期，格式 ：yyyyMMdd
  late String? calendar;

  /// 时间， 单位： 分钟。比如 930 = 15:30
  late int? time;

  /// 压力等级
  /// 4 -> height <br/>
  /// 3 -> medium <br/>
  /// 2 -> normal <br/>
  /// 1 -> low <br/>
  late int? level;

  /// 压力值
  @ColumnInfo(name: 'pressure_value')
  late int? pressureValue;

  late int? maxPressureValue;

  late int? minPressureValue;

  StressModel({
    super.id,
    super.address,
    super.dateCreated,
    super.dateModified,
    this.calendarTime,
    this.calendar,
    this.time,
    this.level,
    this.pressureValue,
  });

  @override
  bool from(Map map) {
    // TODO: implement from
    throw UnimplementedError();
  }

  @override
  List<Object?> get props => [calendarTime, calendar, time, level, pressureValue];

  @override
  bool? get stringify => true;

  setLevel() {
    int pv = pressureValue ?? 0;
    if (pv > 79) {
      level = 4;
    } else if (pv > 59) {
      level = 3;
    } else if (pv > 29) {
      level = 2;
    } else {
      level = 1;
    }
  }

  /// 获取当前压力文本
  get stressText {
    if (null == pressureValue || pressureValue == 0) {
      return LocaleKeys.no_records_text.ts;
    }

    String levelText = '';
    if (level == 4) {
      levelText = LocaleKeys.stress_quality_height_text.ts;
    } else if (level == 3) {
      levelText = LocaleKeys.stress_quality_medium_text.ts;
    } else if (level == 2) {
      levelText = LocaleKeys.stress_quality_normal_text.ts;
    } else {
      levelText = LocaleKeys.stress_quality_low_text.ts;
    }

    return '$pressureValue $levelText';
  }

  get tooltipText {
    // return '$stressText\n';
    return '${LocaleKeys.widgets_stress.ts}: $pressureValue\n';
  }
}

class StressSummary implements Equatable {
  late String calendar;
  late String calendarTime;
  late int avgPressureValue;
  late int maxPressureValue;
  late int minPressureValue;
  late List<StressModel> summaryList;

  StressSummary({this.calendar = '', this.calendarTime = '', this.summaryList = const []});

  @override
  // TODO: implement props
  List<Object?> get props => [calendar, calendarTime, summaryList];

  @override
  // TODO: implement stringify
  bool? get stringify => true;
}
